/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package diet.XMLUtils;

import java.io.BufferedOutputStream;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.MalformedURLException;
import java.util.logging.Level;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.sax.SAXResult;
import javax.xml.transform.stream.StreamSource;
import org.apache.avalon.framework.logger.ConsoleLogger;
import org.apache.avalon.framework.logger.Logger;
import org.apache.fop.apps.FOPException;
import org.apache.fop.apps.FOUserAgent;
import org.apache.fop.apps.Fop;
import org.apache.fop.apps.FopFactory;
import org.apache.fop.apps.MimeConstants;
import org.xml.sax.InputSource;
import org.apache.fop.apps.FopFactory;
import org.apache.fop.apps.Fop;
import org.apache.fop.apps.MimeConstants;

/**
 *
 * @author Vrety
 */
public class PDFGenerating {

    public PDFGenerating() {
    }

    // input file FO
    // output file PDF
//    public static boolean CreatePDF(File inputFile, File pdfFile) {
//        InputStream input = null;
//        OutputStream output = null;
//        try {
//            Driver driver = new Driver();
//            Logger logger = new ConsoleLogger(ConsoleLogger.LEVEL_INFO);
//            driver.setLogger(logger);
//            driver.setRenderer(Driver.RENDER_PDF);
//            input = new FileInputStream(inputFile);
//            //set file FO
//            driver.setInputSource(new InputSource(input));
//            // set file PDF
//            output = new FileOutputStream(pdfFile);
//            driver.setOutputStream(output);
//            driver.run();
//            output.flush();
//            output.close();
//
//
//            return true;
//        } catch (IOException ex) {
//            ex.printStackTrace();
//            return false;
//        } catch (FOPException ex) {
//            ex.printStackTrace();
//            return false;
//        } finally {
//            try {
//                //System.out.println("!!!");
//                input.close();
//            } catch (IOException ex) {
//                ex.printStackTrace();
//            }
//        }
//    }

    /* inputFile: file FO
     * pdfFile: file PDF
     * @Author Vrety
     */
    public static boolean CreatePDF(File inputFile, File pdfFile, String realPath) {

        OutputStream output = null;
        try {
            // Step 1: Construct a FopFactory
            FopFactory fopFactory = FopFactory.newInstance();

            //Set baseURL for display Image in file FO.
            fopFactory.setBaseURL(realPath);

            FOUserAgent foUserAgent = fopFactory.newFOUserAgent();
            foUserAgent.setBaseURL(fopFactory.getBaseURL());
            //VuongHTT
            File config = new File(realPath+"xml/myconfig.xconf");
            fopFactory.setUserConfig(config);

            // Step 2: Set up output stream.
            output = new BufferedOutputStream(new FileOutputStream(pdfFile));

            // Step 3: Construct fop with desired output format
            Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, foUserAgent, output);

            // Step 4: Setup JAXP using identity transformer
            TransformerFactory factory = TransformerFactory.newInstance();
            Transformer transformer = factory.newTransformer(); // identity transformer

            // Step 5: Setup input and output for XSLT transformation
            // Setup input stream
            Source src = new StreamSource(inputFile);

            // Resulting SAX events (the generated FO) must be piped through to FOP
            Result res = new SAXResult(fop.getDefaultHandler());

            // Step 6: Start XSLT transformation and FOP processing
            transformer.transform(src, res);

            return true;
        } catch (MalformedURLException ex) {
            java.util.logging.Logger.getLogger(PDFGenerating.class.getName()).log(Level.SEVERE, null, ex);
        } catch (TransformerException ex) {
            java.util.logging.Logger.getLogger(PDFGenerating.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        } catch (FOPException ex) {
            java.util.logging.Logger.getLogger(PDFGenerating.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        } catch (FileNotFoundException ex) {
            java.util.logging.Logger.getLogger(PDFGenerating.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        } catch (IOException ex) {
            java.util.logging.Logger.getLogger(PDFGenerating.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        } finally {
            try {
                output.close();
                return true;
            } catch (IOException ex) {
                java.util.logging.Logger.getLogger(PDFGenerating.class.getName()).log(Level.SEVERE, null, ex);
                return false;
            }
        }
    }
}
